﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using Cqrs.Infrastructure.Aggregate;

namespace Cqrs.StateMachineDesigner.CustomCode
{
    /// <summary>
    /// Interaction logic for SelectDomainType.xaml
    /// </summary>
    public partial class SelectDomainType : Window
    {

        private Type _returnType;

        public SelectDomainType()
        {
            InitializeComponent();
        }

        public Type select(Assembly assembly)
        {
            this.lstTypes.ItemsSource = assembly.GetTypes()
                .Where(t => t.IsSubclassOf(typeof(AggregateRootWithState)))
                .Where(t => !t.IsAbstract)
                .ToList();
            this.ShowDialog();
            return _returnType;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }


        private void Select()
        {
            if (lstTypes.SelectedItem == null)
                return;
            _returnType = (Type)lstTypes.SelectedItem;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void lstTypes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
